﻿#include "CAlgorithmVoiceHy.h"
#include "CHeader.h"
#include "CLogOper.h"
#include <QtGlobal>
#include "CSqlOper.h"
//#include "IFAlarm.h"
CAlgorithmVoiceHy::CAlgorithmVoiceHy(QString sSceneId)
{
	m_sGuid = sSceneId;
}

CAlgorithmVoiceHy::~CAlgorithmVoiceHy()
{
	killTimer(m_nExecTimer);
}

bool CAlgorithmVoiceHy::actionAlgorthm(SCPolItemCom * pPolItemCom, QString sTaskName)
{
	m_pRBasicData->m_pMutex->lock();
	CPolItemCamer *pPolItemCamer = (CPolItemCamer*)pPolItemCom->m_pPolItem;
	if (pPolItemCamer->m_nCheckType == 1)
	{
		if (pPolItemCamer->m_nCollectIndex == 0)
		{
			m_bConllectBegin = true;
			m_pPolItemCom = pPolItemCom;
			m_nExecTimer = startTimer(50);
			pPolItemCom->m_bIsSave = true;
		}
		if (pPolItemCamer->m_nCollectIndex == 1)
		{
			pPolItemCom->m_bIsSave = true;
			killTimer(m_nExecTimer);
		}
		if (pPolItemCamer->m_nCollectIndex == 2)
		{
			m_pPolItemCom = pPolItemCom;
			pPolItemCom->m_bIsSave = true;
		}
		m_pRBasicData->m_pMutex->unlock();
	}
	else
	{
		m_pRBasicData->m_pMutex->unlock();
		execAlgorthm(pPolItemCom, sTaskName);
	}
	return true;
}

//关闭连续检测
void CAlgorithmVoiceHy::closeCotDet()
{
	m_pPolItemCom = nullptr;
	if (m_nExecTimer != 0)
	{
		killTimer(m_nExecTimer);
	}
}

bool CAlgorithmVoiceHy::execAlgorthm()
{
	m_pRBasicData->m_pMutex->lock();
	if (m_pPolItemCom == nullptr)
	{
		m_pRBasicData->m_pMutex->unlock();
		return false;
	}
	if (m_pPolItemCom->m_pPolItem == nullptr)
	{
		m_pRBasicData->m_pMutex->unlock();
		return false;
	}

	try
	{
		/**
	* @ Author: Gaojia
	* @ Description: 获取基础数据
	*/
		CDevDataBase* pDevDataRob = (CDevDataBase*)m_pDev->m_pDevDataBase;
		CPolItemCamer *pPolItemCamer = (CPolItemCamer*)m_pPolItemCom->m_pPolItem;//云台巡检项
		CDevDataCam* pDataDevCamer = pDevDataRob->m_pDevDataCam;
		double dCurrLocation = pDevDataRob->m_sRobCurrLoca.toFloat() / 1000;
		double dRealLoaction = pPolItemCamer->m_dRollerOffdetDis + dCurrLocation;
		double dValue = pDataDevCamer->m_dAtcDecibel;
		QString sRollerDesc = "";   //托辊名称
		int nRollerNum = 0;         //托辊序号
		nRollerNum = pDevDataRob->getCurrRollerNo(sRollerDesc);//连续检测结果的当前所在托辊号
		/**
		* @ Author: Gaojia
		* @ Description: 1.判断是否需要采集数据
		*/
		if (pPolItemCamer->m_nCollectCheckType == 0)
		{
			double dInterval = pPolItemCamer->m_dCollectInterval;
			if (!isCollectMeter(nRollerNum, dInterval))
			{
				m_pRBasicData->m_pMutex->unlock();
				return false;
			}
		}
		else
		{
			int nInterval = pPolItemCamer->m_nCollectRollerNum;
			if (!isCollectRoller(nRollerNum, nInterval))
			{
				m_pRBasicData->m_pMutex->unlock();
				return false;
			}
		}

		/**
		* @ Author: Gaojia
		* @ Description: 3.生成数据
		*/
		CItemResultCamera *pItemResultCamer = (CItemResultCamera *)m_pPolItemCom->m_ItemResult;//相机结果
		CItemResultCamera *pItemResultCamerCon = new CItemResultCamera();
		pItemResultCamer->copyData(pItemResultCamerCon);
		pItemResultCamerCon->m_sItemResultGuid = createGuid();//生成新的巡检项结果Guid
		pItemResultCamerCon->m_dActionTime = QDateTime::currentDateTime();  //执行时间
		pItemResultCamerCon->m_dValue = dValue;
		pItemResultCamerCon->m_dRealLocation = dCurrLocation;
		pItemResultCamerCon->m_sRollerDesc = sRollerDesc;
		pItemResultCamerCon->m_nRollerNum = nRollerNum;
		pItemResultCamerCon->m_nBelong = pPolItemCamer->m_nBelong;
		pItemResultCamerCon->m_sTaskName = m_pRBasicData->m_pTaskExecInfo->m_sTaskName;
		m_pPolItemCom->m_pPolPoint->m_sRollerDesc = sRollerDesc;
		m_pPolItemCom->m_pPolPoint->m_nRollerCode = nRollerNum;
		getAlarmData(pItemResultCamerCon);
		pItemResultCamerCon->m_sItemName = m_pPolItemCom->m_pPolPoint->m_sRollerDesc + "-" + A2T("声音检测");
		if (pItemResultCamerCon->m_nState != 0)
		{
			//更新上次发生报警位置
			m_dLastLocation = dCurrLocation;
			m_pRBasicData->m_pTaskExecInfo->m_nCollectAlarmCount++;
		}
		m_pRBasicData->m_pTaskExecInfo->m_nCollectExecCount++;
		QString sGuid = pItemResultCamerCon->m_sTaskExecGuid;
		int nAlarmCount = m_pRBasicData->m_pTaskExecInfo->m_nCollectAlarmCount;
		int nExecCount = m_pRBasicData->m_pTaskExecInfo->m_nCollectExecCount;
		m_pRBasicData->m_pMutex->unlock();
		updateCollectCountEx(sGuid, nAlarmCount, nExecCount);
		insertCamerItemResult(pItemResultCamerCon);
		m_pRBasicData->m_pMutex->lock();
		m_pPolItemCom->m_vecItemResult.push_back(pItemResultCamerCon);
		/**
		* @ Author: Gaojia
		* @ Description: 更新报警牌数据
		*/
		/**
		* @ Author: Gaojia
		* @ Description: 5.更新报警牌数据
		*/
		QString sAlarmDesc = A2T("声音超分贝报警");
		QString sVal = QString::number(pItemResultCamerCon->m_dValue) + "db";
		m_pRBasicData->m_pMutex->unlock();
		//upAlarmDataInfo(sAlarmDesc, sVal, 1, pItemResultCamerCon);
		return true;
	}
	catch (const std::exception&)
	{
		m_pRBasicData->m_pMutex->unlock();
		return false;
	}
}


bool CAlgorithmVoiceHy::execAlgorthm(SCPolItemCom * pPolItemCom, QString sTaskName)
{
	m_pRBasicData->m_pMutex->lock();
	CPolItemCamer *pPolItemCamer = (CPolItemCamer*)pPolItemCom->m_pPolItem;//云台巡检项
	//取出红外测温结果
	CDevDataBase* pDevDataRob = (CDevDataBase*)m_pDev->m_pDevDataBase;
	CDevDataCam* pDataDevCamer = pDevDataRob->m_pDevDataCam;
	CItemResultCamera *pItemResultCamer = (CItemResultCamera *)pPolItemCom->m_ItemResult;  //相机结果
	double dCurrLocation = pDevDataRob->m_sRobCurrLoca.toFloat() / 1000;
	double dValue = pDataDevCamer->m_dAtcDecibel;
	QString sRollerDesc = "";   //托辊名称
	int nRollerNum = 0;         //托辊序号
	nRollerNum = pDevDataRob->getCurrRollerNo(sRollerDesc);//连续检测结果的当前所在托辊号

	/**
	* @ Author: Gaojia
	* @ Description: 生成数据
	*/
	pItemResultCamer->m_dValue = dValue;
	pItemResultCamer->m_dActionTime = QDateTime::currentDateTime();
	pItemResultCamer->m_dRealLocation = dCurrLocation;
	pItemResultCamer->m_nRollerNum = nRollerNum;
	pItemResultCamer->m_sRollerDesc = sRollerDesc;
	pItemResultCamer->m_nBelong = pPolItemCamer->m_nBelong;
	pItemResultCamer->m_sHttpUrl = "";
	pPolItemCom->m_pPolPoint->m_sRollerDesc = sRollerDesc;
	pPolItemCom->m_pPolPoint->m_nRollerCode = nRollerNum;

	getAlarmData(pItemResultCamer);

	if (pItemResultCamer->m_nState != 0)
	{
		m_pRBasicData->m_pTaskExecInfo->m_nAlarmItemCount++;
		//发生报警要报警停留
		if (m_pRBasicData->m_nSmallSceneType != 0)//南电没有报警停留
		{
			SCtrInfo info;
			info.m_bCtrSync = true;
			info.m_sGuid = m_sGuid;
			info.m_bHasCtr = false;
			info.m_nCtrDevType = enOntology;
			info.m_nCtrCmd = ECCmdType::enCAlarmRemain;
			m_pDev->ctrDev(info);
		}
		//CSqlOperRob::instance().updateAlmInform(m_sSceneId, enVoice); //更新报警通知表
	}
	/**
	* @ Author: Gaojia
	* @ Description: 更新报警牌数据
	*/
	QString sAlarmDesc = A2T("声音超分贝报警");
	QString sVal = QString::number(pItemResultCamer->m_dValue) + "db";
	m_pRBasicData->m_pMutex->unlock();
	//upAlarmDataInfo(sAlarmDesc, sVal, 0, pItemResultCamer);
	return true;
}

void CAlgorithmVoiceHy::timerEvent(QTimerEvent * event)
{
	if (event->timerId() == m_nExecTimer)
	{
		try
		{
			execAlgorthm();
		}
		catch (const std::exception&)
		{

		}
	}
}

void CAlgorithmVoiceHy::getAlarmData(CItemResultCamera * pItemResultCamera)
{
	CDevDataBase* pDevDataRob = (CDevDataBase*)m_pDev->m_pDevDataBase;
	//取出红外测温结果
	double dValueHH = pItemResultCamera->m_sDataThreadHoldHH.toDouble();
	double dValueH = pItemResultCamera->m_sDataThreadHoldH.toDouble();
	double dValueLL = pItemResultCamera->m_sDataThreadHoldLL.toDouble();
	double dValueL = pItemResultCamera->m_sDataThreadHoldL.toDouble();
	double dValue = pItemResultCamera->m_dValue;

	if (dValue <= dValueLL)//  Val <= 低低限
	{
		pItemResultCamera->m_nState = -2;
		pItemResultCamera->m_nAlarmLevel = 2;
		pItemResultCamera->m_sDataDesc = A2T("%1,%2托辊,发生噪声低低报警,其中报警限%3分贝,报警值%4分贝")
			.arg(pDevDataRob->m_sRobName)
			.arg(pItemResultCamera->m_sRollerDesc)
			.arg(QString::number(dValueLL, 'f', 1))
			.arg(QString::number(dValue, 'f', 1));
		pItemResultCamera->m_sResultDesc = A2T("发生噪声低低报警,其中报警限%1分贝,报警值%1分贝")
			.arg(QString::number(dValueLL, 'f', 1))
			.arg(QString::number(dValue, 'f', 1));
	}
	else if (dValueLL < dValue && dValue <= dValueL)//  低低限 < Val <= 低限
	{
		pItemResultCamera->m_nState = -1;
		pItemResultCamera->m_nAlarmLevel = 1;
		pItemResultCamera->m_sDataDesc = A2T("%1,%2托辊,发生噪声低报警,其中报警限%3分贝,报警值%4分贝")
			.arg(pDevDataRob->m_sRobName)
			.arg(pItemResultCamera->m_sRollerDesc)
			.arg(QString::number(dValueL, 'f', 1))
			.arg(QString::number(dValue, 'f', 1));
		pItemResultCamera->m_sResultDesc = A2T("发生噪声低报警,其中报警限%1分贝,报警值%2分贝")
			.arg(QString::number(dValueL, 'f', 1))
			.arg(QString::number(dValue, 'f', 1));

	}
	else if (dValueL < dValue && dValue <= dValueH)//  低限 < Val <= 高限  (这个代表正常)
	{
		pItemResultCamera->m_nState = 0;
		pItemResultCamera->m_nAlarmLevel = 0;
		pItemResultCamera->m_sDataDesc = A2T("%1,%2托辊,声音检测正常,其中报警限%3分贝,数值%4分贝")
			.arg(pDevDataRob->m_sRobName)
			.arg(pItemResultCamera->m_sRollerDesc)
			.arg(QString::number(dValueH, 'f', 1))
			.arg(QString::number(dValue, 'f', 1));
		pItemResultCamera->m_sResultDesc = A2T("声音检测正常,其中报警限%1分贝,数值%2分贝")
			.arg(QString::number(dValueH, 'f', 1))
			.arg(QString::number(dValue, 'f', 1));
	}
	else if (dValueH < dValue && dValue <= dValueHH)//  高限 < Val <= 高高限
	{
		pItemResultCamera->m_nState = 1;
		pItemResultCamera->m_nAlarmLevel = 1;
		pItemResultCamera->m_sDataDesc = A2T("%1,%2托辊,发生噪声高报警,其中报警限%3分贝,报警值%4分贝")
			.arg(pDevDataRob->m_sRobName)
			.arg(pItemResultCamera->m_sRollerDesc)
			.arg(QString::number(dValueH, 'f', 1))
			.arg(QString::number(dValue, 'f', 1));
		pItemResultCamera->m_sResultDesc = A2T("发生噪声高报警,其中报警限%1分贝,报警值%2分贝")
			.arg(QString::number(dValueH, 'f', 1))
			.arg(QString::number(dValue, 'f', 1));
	}
	else//  高高限 < Val
	{
		pItemResultCamera->m_nState = 2;
		pItemResultCamera->m_nAlarmLevel = 2;
		pItemResultCamera->m_sDataDesc = A2T("%1,%2托辊,发生噪声高高报警,其中报警限%3分贝,报警值%4分贝")
			.arg(pDevDataRob->m_sRobName)
			.arg(pItemResultCamera->m_sRollerDesc)
			.arg(QString::number(dValueHH, 'f', 1))
			.arg(QString::number(dValue, 'f', 1));
		pItemResultCamera->m_sResultDesc = A2T("发生噪声高高报警,其中报警限%1分贝,报警值%2分贝")
			.arg(QString::number(dValueHH, 'f', 1))
			.arg(QString::number(dValue, 'f', 1));
	}
}

